﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyBanHang.DAL;
using QuanLyBanHang.VO;
using QuanLyBanHang.BUS;

namespace QuanLyBanHang
{
    public partial class Form_NV2 : Form
    {
        public bool them = false;
        int manv;
        NhanVien_DAL nv_dal = new NhanVien_DAL();
        NhanVien_BUS nv_bus = new NhanVien_BUS();
        public Form_NV2()
        {
            InitializeComponent();
            //lbl_Ten.Visible = false;
            //lbl_Luong.Visible = false;
            //lbl_DT.Visible = false;
            //lbl_DC.Visible = false;
            //lbl_TaiKhoan.Visible = false;
            //lbl_MK.Visible = false;
            //rbtn_Admin.Visible = false;
            //rbtn_User.Visible = false;
            //txt_Ten.Visible = false;
            //txt_TaiKhoan.Visible = false;
            //txt_MatKhau.Visible = false;
            //txt_Luong.Visible = false;
            //txt_DT.Visible = false;
            //txt_DiaChi.Visible = false;
        }

        private void Form_NV1_Load(object sender, EventArgs e)
        {

        }

        private void Form_NV1_Load_1(object sender, EventArgs e)
        {
            //dataGridView1.DataSource = nv_dal.Select(textBox_tk.Text);
            //dataGridView1.ReadOnly = true;
            //dataGridView1.Columns[0].HeaderText = "Mã nhân viên";
            //dataGridView1.Columns[1].HeaderText = "Họ Tên";
            //dataGridView1.Columns[2].HeaderText = "Lương";
            //dataGridView1.Columns[3].HeaderText = "Địa chỉ";
            //dataGridView1.Columns[4].HeaderText = "Số điện thoại";
            //dataGridView1.Columns[5].HeaderText = "Tài khoản";
            //dataGridView1.Columns[6].HeaderText = "Mật khẩu";
            //dataGridView1.Columns[7].HeaderText = "Quyền";
            XemNv();
        }
        public void XemNv()
        {
            dataGridView1.Rows.Clear();
            NhanVien_DAL NV_dal = new NhanVien_DAL();
            LinkedList<NhanVien_VO> List_NV = new LinkedList<NhanVien_VO>();
            List_NV = NV_dal.Show_NV();
            foreach (NhanVien_VO nv in List_NV)
            {
                dataGridView1.Rows.Add(nv.MaNV, nv.hoten, nv.luong, nv.diachi, nv.sodt, nv.taikhoan, nv.matkhau, nv.quyen);
            }
        }

        private void button_tk_Click(object sender, EventArgs e)
        {
            //dataGridView1.DataSource = nv_dal.Select(textBox_tk.Text);
            NhanVien_DAL NV_dal = new NhanVien_DAL();
            dataGridView1.Rows.Clear();
            DataTable dt = new DataTable();
            dt = NV_dal.Select(textBox_tk.Text);
            NhanVien_VO nv = new NhanVien_VO();
            LinkedList<NhanVien_VO> List_NV = new LinkedList<NhanVien_VO>();
            try
            {
                for(int i=0; i < dt.Rows.Count; i++)
                {
                    nv.MaNV = int.Parse(dt.Rows[i][0].ToString().Trim());
                    nv.hoten = dt.Rows[i][1].ToString().Trim();
                    nv.luong = int.Parse(dt.Rows[i][2].ToString().Trim());
                    nv.diachi = dt.Rows[i][3].ToString().Trim();
                    nv.sodt = dt.Rows[i][4].ToString().Trim();
                    nv.taikhoan = dt.Rows[i][5].ToString().Trim();
                    nv.matkhau = dt.Rows[i][6].ToString().Trim();
                    nv.quyen = int.Parse(dt.Rows[i][7].ToString().Trim());
                    List_NV.AddLast(nv);
                    nv = new NhanVien_VO();
                }
                foreach(NhanVien_VO nv1 in List_NV)
                {
                    dataGridView1.Rows.Add(nv1.MaNV, nv1.hoten, nv1.luong, nv1.sodt, nv1.taikhoan, nv1.matkhau, nv1.quyen);
                }
            }
            catch
            {
                MessageBox.Show("Không tìm thấy");
            }
        }
        public void cleartxt()
        {
            txt_DiaChi.Text = "";
            txt_DT.Text ="";
            txt_Luong.Text ="";
            txt_Manv.Text ="";
            txt_MatKhau.Text ="";
            txt_TaiKhoan.Text = "";
            txt_Ten.Text ="";
        }
        private void button_add_Click(object sender, EventArgs e)
        {
            them = true;
            cleartxt();
            button_add.Enabled = false;
            button_edit.Enabled = false;
            button_cancel.Enabled = true;
            button_del.Enabled = false;
            button_save.Enabled = true;

            lbl_Ten.Visible = true;
            lbl_Luong.Visible = true;
            lbl_DT.Visible = true;
            lbl_DC.Visible = true;
            lbl_TaiKhoan.Visible = true;
            lbl_MK.Visible = true;
            rbtn_Admin.Visible = true;
            rbtn_User.Visible = true;
            rbtn_User.Checked = true;
            if (rbtn_Admin.Checked == true)
            {
                rbtn_User.Checked = false;
            }
            else if (rbtn_User.Checked == true)
            {
                rbtn_Admin.Checked = false;
            }
            rbtn_User.Checked = true;
            txt_Ten.Visible = true;
            txt_TaiKhoan.Visible = true;
            txt_MatKhau.Visible = true;
            txt_Luong.Visible = true;
            txt_DT.Visible = true;
            txt_DiaChi.Visible = true;
            dataGridView1.ReadOnly = false;
            dataGridView1.Columns[0].ReadOnly = true;
        }

        private void button_edit_Click(object sender, EventArgs e)
        {
            //button_add.Enabled = false;
            //button_edit.Enabled = false;
            //button_cancel.Enabled = true;
            //button_del.Enabled = false;
            //button_save.Enabled = true;
            //dataGridView1.ReadOnly = false;
            //dataGridView1.Columns[0].ReadOnly = true;
            //dataGridView1.Columns[7].ReadOnly = true;
            //manv = int.Parse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
            NhanVien_DAL nv_dal = new NhanVien_DAL();
            NhanVien_VO nv = new NhanVien_VO();
            if ((txt_Ten.Text == "") || (txt_TaiKhoan.Text) == "")
            {
                MessageBox.Show("Bạn phải điền đầy đủ thông tin");
                return;
            }
            try
            {
                nv.MaNV = int.Parse(txt_Manv.Text);
                nv.hoten = txt_Ten.Text;
                nv.luong = int.Parse(txt_Luong.Text);
                nv.sodt = txt_DT.Text;
                nv.diachi = txt_DiaChi.Text;
                nv.taikhoan = txt_TaiKhoan.Text;
                nv.matkhau = txt_MatKhau.Text;
                if (rbtn_Admin.Checked == true)
                    nv.quyen = 1;
                else nv.quyen = 0;
                string tk = txt_TaiKhoan.Text;              
                if((nv_bus.trungTK(tk)>0)&&(nv_bus.layTK(nv.MaNV).Trim()!=tk)) 
                {
                     MessageBox.Show("tài khoản đã tồn tại");
                }
                else
                {
                    if (nv_dal.update(nv.MaNV, nv.hoten, nv.luong, nv.diachi, nv.sodt, nv.taikhoan, nv.matkhau, nv.quyen)) MessageBox.Show("Thành công");
                    else MessageBox.Show("Có lỗi");
                }
                XemNv();
            }
            catch
            {
                MessageBox.Show("Vui lòng chọn nhân viên cần sửa");
            }
            
        }

        private void button_save_Click(object sender, EventArgs e)
        {
            if (them)
            {
                if ((txt_Ten.Text == "") || (txt_TaiKhoan.Text) == "")
                {
                    MessageBox.Show("Bạn phải điền đầy đủ thông tin");
                    return;
                }
                int quyen;
                if (rbtn_Admin.Checked == true)
                    quyen = 1;
                else quyen = 0;
                    try 
                    {
                        string tk = txt_TaiKhoan.Text;
                        if(nv_bus.trungTK(tk)>0) 
                        {
                            MessageBox.Show("tài khoản đã tồn tại");
                        }
                        else
                        {
                            nv_dal.insert(txt_Ten.Text, int.Parse(txt_Luong.Text), txt_DiaChi.Text, txt_DT.Text, txt_TaiKhoan.Text, txt_MatKhau.Text, quyen);
                            MessageBox.Show("Thành công");
                            them = false;
                            button_add.Enabled = true;
                            button_edit.Enabled = true;
                            button_cancel.Enabled = false;
                            button_del.Enabled = true;
                            button_save.Enabled = false;
                        }
                    }
                    catch
                    {
                        MessageBox.Show("Có lỗi, vui lòng nhập lại");
                    }
                XemNv();
            }
            //else
            //{
            //    //int n = dataGridView1.CurrentRow.Index;
            //    ////MessageBox.Show(n.ToString());
            //    //string ten = dataGridView1.Rows[n].Cells[1].Value.ToString();
            //    //int luong = int.Parse(dataGridView1.Rows[n].Cells[2].Value.ToString());
            //    //string dc = dataGridView1.Rows[n].Cells[3].Value.ToString();
            //    //string sodt = dataGridView1.Rows[n].Cells[4].Value.ToString();
            //    //string tk = dataGridView1.Rows[n].Cells[5].Value.ToString();
            //    //string matkhau = dataGridView1.Rows[n].Cells[6].Value.ToString();
            //    //int quyen = int.Parse(dataGridView1.Rows[n].Cells[7].Value.ToString());
            //    if (nv_dal.update(manv, ten, luong, dc, sodt, tk, matkhau, quyen)) MessageBox.Show("Thành công");
            //    else MessageBox.Show("Có lỗi");
            //}
            else
            {
                them = false;
                button_add.Enabled = true;
                button_edit.Enabled = true;
                button_cancel.Enabled = false;
                button_del.Enabled = true;
                button_save.Enabled = false;
            }
            
            //dataGridView1.ReadOnly = true;
            //dataGridView1.DataSource = nv_dal.Select(textBox_tk.Text);
        }

        private void button_cancel_Click(object sender, EventArgs e)
        {
            them = false;
            button_add.Enabled = true;
            button_edit.Enabled = true;
            button_cancel.Enabled = false;
            button_del.Enabled = true;
            button_save.Enabled = false;
            dataGridView1.ReadOnly = true;
        }

        private void button_del_Click(object sender, EventArgs e)
        {
            
            try
            {
                int manv = int.Parse(txt_Manv.Text);
                nv_dal.delete(manv);
                MessageBox.Show("Đã xóa");
            }
            catch
            {
                MessageBox.Show("Có lỗi");
            }
            XemNv();
        }

        private void button_exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txt_Luong_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void txt_DT_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txt_Manv.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            txt_Ten.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            txt_Luong.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
            txt_DiaChi.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
            txt_DT.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
            txt_TaiKhoan.Text = dataGridView1.CurrentRow.Cells[5].Value.ToString();
            txt_MatKhau.Text = dataGridView1.CurrentRow.Cells[6].Value.ToString();
            if (dataGridView1.CurrentRow.Cells[7].Value.ToString() == "1")
                rbtn_Admin.Checked = true;
            if (dataGridView1.CurrentRow.Cells[7].Value.ToString() == "0")
                rbtn_User.Checked = true;
        }

        private void btn_Xem_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();
            NhanVien_DAL NV_dal = new NhanVien_DAL();
            LinkedList<NhanVien_VO> List_NV = new LinkedList<NhanVien_VO>();
            List_NV = NV_dal.Show_NV();
            foreach (NhanVien_VO nv in List_NV)
            {
                dataGridView1.Rows.Add(nv.MaNV, nv.hoten, nv.luong, nv.diachi, nv.sodt, nv.taikhoan, nv.matkhau, nv.quyen);
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
